package tag.in.activities;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import tag.in.Constants;
import tag.in.R;
import tag.in.TagInApplication;
import tag.in.data.Tag;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import com.google.android.maps.Overlay;

public class TagMapActivity extends MapActivity {  
	private static final String CLASSTAG = TagMapActivity.class.getSimpleName();
	
	/** Called when the activity is first created. */
	MapView mapView; 
	MapController mc;
	GeoPoint p;
	
	

	class MapOverlay extends com.google.android.maps.Overlay
	{
		@Override
		public boolean draw(Canvas canvas, MapView mapView, 
				boolean shadow, long when) 
		{
			super.draw(canvas, mapView, shadow);                   

			//---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(p, screenPts);

			//---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(
					getResources(), R.drawable.pushpin);            
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
			return true;
		}

		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) 
		{   
			//---when user lifts his finger---
			if (event.getAction() == 1) {  
				String add = "entrou aki";
				GeoPoint p = mapView.getProjection().fromPixels(
						(int) event.getX(),
						(int) event.getY());
				
				
				Geocoder geoCoder = new Geocoder(
						getBaseContext(), Locale.getDefault());
				
				try {
					List<Address> addresses = geoCoder.getFromLocation(
							(double)p.getLatitudeE6()/1E6, 
							(double)p.getLongitudeE6()/1E6, 1);

					Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
					if (addresses.size() > 0) 
					{
						
						for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
						i++)
							add += addresses.get(0).getAddressLine(i) + "\n";
					}

					//Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
					
				}
				catch (IOException e) { 
					Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}   
				return true;
			}
			else                
				return false;
		}        
	}



	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.v(Constants.LOGTAG, " " + TagMapActivity.CLASSTAG + " onCreate");
		
		setContentView(R.layout.tag_map);

		mapView = (MapView) findViewById(R.id.mapView);
		//mapView.setStreetView(true);
		//LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
		mapView.setBuiltInZoomControls(true);

		// get the current review from the Application (global state placed there)
        TagInApplication application = (TagInApplication) getApplication();
		Tag currentTag = application.getCurrentTag();
		
		
		
		mc = mapView.getController(); //obtain a controller from the MapView instance and assign it to a MapController object (mc)
		String coordinates[] = {"40.186673", "-8.416241"}; //
		double lat = currentTag.lat;	//Double.parseDouble(coordinates[0]);
		double lng = currentTag.lon;	//Double.parseDouble(coordinates[1]);

		p = new GeoPoint(
				(int) (lat * 1E6), 
				(int) (lng * 1E6));  // latitude e longitude em micro degrees(valor inteiro)

		mc.animateTo(p);
		mc.setZoom(17);

		//---Add a location marker---
		MapOverlay mapOverlay = new MapOverlay();
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);     


		mapView.invalidate();



	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		// MapController mc = mapView.getController(); 
		//mc.
		
		
		switch (keyCode) 
		{
		case KeyEvent.KEYCODE_1:
			mapView.setSatellite(!mapView.isSatellite());
			break;
		}
		return super.onKeyDown(keyCode, event);
	}   
}
